
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

# Data for Table_A5
data = {'Acts on citizen feedback': [-0.035, -0.034, 0.188, -0.003], 'Takes citizen report seriously': [0.01, -0.048, 0.068, 0.014], 'Responds timely': [-0.05, 0.014, 0.108, -0.043], 'Not corrupt': [0.084, -0.143, -0.069, -0.036], 'Provides equal service': [-0.137, -0.04, 0.15, -0.056], 'I trust the police': [-0.066, 0.08, 0.061, -0.014], 'Provides satisfactory service': [0.043, -0.047, 0.063, -0.04]}

# Standard errors
std_errors = {'Acts on citizen feedback': [0.066, 0.061, 0.068, 0.071], 'Takes citizen report seriously': [0.07, 0.056, 0.062, 0.072], 'Responds timely': [0.101, 0.074, 0.074, 0.086], 'Not corrupt': [0.09, 0.086, 0.081, 0.079], 'Provides equal service': [0.08, 0.059, 0.065, 0.073], 'I trust the police': [0.05, 0.047, 0.048, 0.052], 'Provides satisfactory service': [0.069, 0.065, 0.057, 0.069]}

# Independent variable labels
iv_labels = ['Public Service Motivation', 'Cognitive Ability', 'Grit Scale', 'Big 5 Personality Index']

# Convert the data and standard errors to DataFrames
df = pd.DataFrame(data, index=iv_labels)
df_se = pd.DataFrame(std_errors, index=iv_labels)

# Function to calculate T-values from coefficients and standard errors
def calculate_t_values(df, df_se):
    return df / df_se

# Calculate T-values
t_values_df = calculate_t_values(df, df_se)

# Generate significance annotations based on T-values
annotations = t_values_df.applymap(lambda x: '***' if abs(x) > 2.576 else '**' if abs(x) > 1.96 else '*' if abs(x) > 1.645 else '')

# Plotting the heatmap
plt.figure(figsize=(12, 6))

# Plot the heatmap with significance annotations and black-and-white gradient
ax = sns.heatmap(t_values_df.astype(float), annot=annotations, fmt="", cmap="Greys", center=0,
                 linewidths=0.5, cbar_kws={'label': 'T-values', 'orientation': 'horizontal'}, mask=t_values_df.isnull())

# Adjust labels and angles
ax.xaxis.tick_top()
ax.xaxis.set_label_position('top')
ax.set_xticklabels(t_values_df.columns, rotation=45, ha="center", fontsize=10)
ax.set_yticklabels(t_values_df.index, rotation=0, fontsize=10)

# Position the color bar (legend) at the bottom
cbar = ax.collections[0].colorbar
cbar.ax.xaxis.set_label_position('bottom')
cbar.ax.xaxis.set_ticks_position('bottom')

# Adjust layout
plt.tight_layout()
plt.show()
